<?php 
namespace App\Http\Controllers\OAuth;

use App\Ext\Curl;
use App\Model\User;
use App\Http\Controllers\Controller;
use Session;

class QQCallback extends Controller
{

	private $ClientID;

	private $ClientSecret;

	public function __construct()
	{
		$this->middleware('OAuth20');
		$this->ClientID = config('OAuth.QQ.ClientID');
		$this->ClientSecret = config('OAuth.QQ.ClientSecret');
	}

	public function Index()
	{
		$redirect_uri =  urlencode(config('OAuth.QQ.redirect_uri'));
		$url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id='.$this->ClientID.'&client_secret='.$this->ClientSecret.'&code='.$_GET['code'].'&redirect_uri='.$redirect_uri;
		$token = Curl::New_Ch()->Default_Get_Set()->Exec($url);
		parse_str($token, $output);
		$url = 'https://graph.qq.com/oauth2.0/me?access_token='.$output['access_token'];
		$OpenID = Curl::New_Ch()->Default_Get_Set()->Exec($url);
		$str = substr($OpenID, 10, -3);
		$json = json_decode($str, true);
		$url = 'https://graph.qq.com/user/get_user_info?access_token='.$output['access_token'].'&oauth_consumer_key='.$this->ClientID.'&openid='.$json['openid'];
		$Info = Curl::New_Ch()->Default_Get_Set()->Exec($url);
		$Info = json_decode($Info);
		$User_Info['name'] = $Info->nickname;
		$User_Info['oauthid'] = $json['openid'];
		$User_Info['id'] = User::Add_User($User_Info);
		session($User_Info);
		return redirect('/');
	}


}